//
//  DPWalletBillBaseViewController.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2021/3/9.
//

import UIKit

class DPWalletBillBaseViewController: BHBaseViewController {
    
    lazy var changeHeaderView: DPWalletAllBillHeaderView = {
        let view = DPWalletAllBillHeaderView.loadFromXib()
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: 35)
        view.currentIndex = 0
        return view
    }()
    
    lazy var verticalScrollView: UIScrollView = {
        let view = UIScrollView.init(frame: CGRect(x: 0, y: UIScreen.begainY+35, width: UIScreen.width, height: UIScreen.showHeight-35))
        view.delegate = self
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.bounces = false
        view.isPagingEnabled = true
        view.contentSize = CGSize(width: 3*UIScreen.width, height: 0)
        return view
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.title_label.text = "全部账单"
        
        self.view.addSubview(self.changeHeaderView)
        
        
        self.layoutInit()
        
        self.view.addSubview(self.verticalScrollView)
        self.changeHeaderView.didChangeIndexBlock = {[weak self] (index) in
            self?.verticalScrollView.setContentOffset(CGPoint(x: CGFloat(index)*UIScreen.width, y: 0), animated: false)
            
            self?.changeState(index)
        }
        
    }
    
    func layoutInit() {
        for i in 0..<3 {
            let billVC = DPWalletAllBillViewController.init()
            billVC.type = "\(i)"
            billVC.view.frame = CGRect(x:CGFloat(i)*UIScreen.width, y: 0, width: UIScreen.width, height: self.verticalScrollView.bh_height)
            
            self.verticalScrollView.addSubview(billVC.view)
            self.addChild(billVC)
            
            if i == 0 {
                billVC.refresh()
            }
        }
    }
    
    func changeState(_ index: Int) {
        let billVC = self.children[index] as! DPWalletAllBillViewController
        billVC.refresh()
    }

  

}
extension DPWalletBillBaseViewController: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.verticalScrollView {
            let index = NSInteger(scrollView.contentOffset.x/UIScreen.width)
            self.changeState(index)
            self.changeHeaderView.currentIndex = index
        }
    }
}
